package me.itsoo.artemis.framework.repository.mapper.resource;

import me.itsoo.artemis.framework.core.tuple.SimplePair;
import me.itsoo.artemis.framework.core.util.Assert;
import me.itsoo.artemis.framework.core.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * CreateResource
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/12
 */
public class CreateResource extends AbstractResource {

    private final String table;

    private final List<CreateResource> chain;

    private final SimplePair<String, Object> src = new SimplePair<>();

    /**
     * 首节点构造
     *
     * @param table String
     */
    private CreateResource(String table) {
        this.chain = new ArrayList<>();
        this.chain.add(this);
        this.table = table;
    }

    /**
     * 连接点构造
     *
     * @param table String
     */
    private CreateResource(List<CreateResource> chain, String table) {
        this.chain = chain;
        this.chain.add(this);
        this.table = table;
    }

    /**
     * insert
     *
     * @param table String
     * @return CreateResource
     */
    public static CreateResource insert(String table) {
        Assert.isNotBlank(table, "'table' cannot be empty");
        return new CreateResource(table);
    }

    /**
     * add
     *
     * @param field String
     * @param value Object
     * @return CreateResource
     */
    public CreateResource add(String field, Object value) {
        Assert.isNotBlank(field, "'field' cannot be empty");
        final CreateResource result = new CreateResource(this.chain, this.table);
        result.src.setKey(field);
        result.src.setValue(value);
        return result;
    }

    @Override
    public String getResourceCommand() {
        final StringJoiner result = new StringJoiner(" ");
        if (CollectionUtils.isEmpty(chain)) {
            return result.toString();
        }

        result.add("insert");
        result.add("into");
        result.add(chain.get(0).table);
        result.add("(");

        for (int i = 1, len = chain.size(), last = len - 1; i < len; i++) {
            final CreateResource cr = chain.get(i);
            String sp = "";
            if (i < last) {
                sp = ",";
            }
            result.add(cr.src.getKey().concat(sp));
        }

        result.add(")");
        result.add("values");
        result.add("(");

        for (int i = 1, len = chain.size(), last = len - 1; i < len; i++) {
            String sp = "";
            if (i < last) {
                sp = ",";
            }
            result.add("?".concat(sp));
        }

        result.add(")");
        return result.toString();
    }

    @Override
    public Object[] getResourceArgs() {
        final List<Object> result = new ArrayList<>(chain.size());
        if (CollectionUtils.isEmpty(chain)) {
            return result.toArray();
        }

        for (int i = 1, len = chain.size(); i < len; i++) {
            final CreateResource cr = chain.get(i);
            result.add(cr.src.getValue());
        }

        return result.toArray();
    }
}
